package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.OutcomesClassifyRelate;
import com.innovations.inn.service.IOutcomesClassifyRelateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 成果分类对应Controller
 *
 * @author dp
 * @date 2024-08-10
 */
@Api(tags = "成果分类对应")
@RestController
@RequestMapping("/inn/outcomes/classify/relate")
public class OutcomesClassifyRelateController extends BaseController {
    @Autowired
    private IOutcomesClassifyRelateService outcomesClassifyRelateService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询成果分类对应列表
     */
    @ApiOperation("查询成果分类对应列表")
    @PreAuthorize("@ss.hasPermi('inn:relate:list')")
    @GetMapping("/list")
    public TableDataInfo list(OutcomesClassifyRelate outcomesClassifyRelate) {
        startPage();
        List<OutcomesClassifyRelate> list = outcomesClassifyRelateService.selectOutcomesClassifyRelateList(outcomesClassifyRelate);
        return getDataTable(list);
    }

    /**
     * 导出成果分类对应列表
     */
    @PreAuthorize("@ss.hasPermi('inn:relate:export')")
    @Log(title = "成果分类对应", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutcomesClassifyRelate outcomesClassifyRelate) {
        List<OutcomesClassifyRelate> list = outcomesClassifyRelateService.selectOutcomesClassifyRelateList(outcomesClassifyRelate);
        ExcelUtil<OutcomesClassifyRelate> util = new ExcelUtil<OutcomesClassifyRelate>(OutcomesClassifyRelate.class);
        util.exportExcel(response, list, "成果分类对应数据");
    }

    /**
     * 获取成果分类对应详细信息
     */
    @ApiOperation("获取成果分类对应详细信息")
    @PreAuthorize("@ss.hasPermi('inn:relate:query')")
    @GetMapping(value = "/{relateId}")
    public AjaxResult getInfo(@PathVariable("relateId") Long relateId) {
        return success(outcomesClassifyRelateService.selectOutcomesClassifyRelateByRelateId(relateId));
    }

    /**
     * 新增成果分类对应
     */
    @ApiOperation("新增成果分类对应")
    @PreAuthorize("@ss.hasPermi('inn:relate:add')")
    @Log(title = "成果分类对应", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody OutcomesClassifyRelate outcomesClassifyRelate) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        outcomesClassifyRelateService.completeData(outcomesClassifyRelate, userId, 1);
        return toAjax(outcomesClassifyRelateService.insertOutcomesClassifyRelate(outcomesClassifyRelate));
    }

    /**
     * 修改成果分类对应
     */
    @ApiOperation("修改成果分类对应")
    @PreAuthorize("@ss.hasPermi('inn:relate:edit')")
    @Log(title = "成果分类对应", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody OutcomesClassifyRelate outcomesClassifyRelate) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        outcomesClassifyRelateService.completeData(outcomesClassifyRelate, userId, 2);
        return toAjax(outcomesClassifyRelateService.updateOutcomesClassifyRelate(outcomesClassifyRelate));
    }

    /**
     * 删除成果分类对应
     */
    @ApiOperation("删除成果分类对应")
    @PreAuthorize("@ss.hasPermi('inn:relate:remove')")
    @Log(title = "成果分类对应", businessType = BusinessType.DELETE)
    @DeleteMapping("/{relateIds}")
    public AjaxResult remove(@PathVariable Long[] relateIds) {
        return toAjax(outcomesClassifyRelateService.deleteOutcomesClassifyRelateByRelateIds(relateIds));
    }
}
